﻿namespace A2DFramework.OSGI.Helps
{
    using A2DFramework.OSGI.BundleContexts;
    using System;
    using System.IO;
    using System.Reflection;

    public class TypeHelper
    {
        public static Type ParseType(string moduleName, string typeModule, string typeFullName)
        {
            string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            if ((moduleName != null) && (moduleName.Trim().Length > 0))
            {
                baseDirectory = BundleContext.Current.GetModuleInfo(moduleName).ModuleRootPath;
            }
            if ((typeModule != null) && (typeModule.Trim().Length > 0))
            {
                baseDirectory = Path.Combine(baseDirectory, typeModule);
            }
            if (File.Exists(baseDirectory))
            {
                return TryGetType(typeFullName, baseDirectory);
            }
            string[] files = Directory.GetFiles(baseDirectory, "*.dll");
            foreach (string str2 in files)
            {
                Type type = TryGetType(typeFullName, str2);
                if (type != null)
                {
                    return type;
                }
            }
            return null;
        }

        private static Type TryGetType(string typeFullName, string dllFile)
        {
            return Assembly.Load(File.ReadAllBytes(dllFile)).GetType(typeFullName);
        }
    }
}

